﻿Public Class SyntaxConfigDialog
    Dim CodeConfig As New ClsCodeConfig
    Dim mKeyword As New Dictionary(Of String, clsKeyWord)
    Dim FileName As String
#Region "配置文件的读写操作"
    ''' <summary>
    ''' 配置文件的读入操作
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub btnLoad_Click() Handles MyOpenSavePanel.Open_Clicked
        Using mOpenFile As New OpenFileDialog
            mOpenFile.InitialDirectory = Application.StartupPath & "\CodeConfig\"
            mOpenFile.Filter = "*.xml(XML)|*.xml"
            If mOpenFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                FileName = mOpenFile.FileName
                CodeConfig = ReadObjectFromXMLFile(GetType(ClsCodeConfig), mOpenFile.FileName)
                btnSyntaxRule.Enabled = True
            Else
                Exit Sub
            End If
        End Using
        With CodeConfig
            '基本信息
            txtTitle.Text = .CodeTitle
            txtLanguage.Text = .Language
            lblBGColor.BackColor = .BackColor
            lblTextColor.BackColor = .BackColor
            lblKeyWordColor.BackColor = .BackColor
            lblRulerColor.BackColor = .BackColor
            lblCommentColor.BackColor = .BackColor

            lblTextColor.ForeColor = .TextColor
            lblBGColor.ForeColor = .TextColor
            nudLeft.Value = .LeftLimit
            nudRight.Value = .RightLimit
            lblRulerColor.ForeColor = .RulerColor

            lblSelectBackColor.BackColor = .SelectBackColor
            lblSelectBackColor.ForeColor = .SelectTextColor

            lblSelectTextColor.BackColor = .SelectBackColor
            lblSelectTextColor.ForeColor = .SelectTextColor


            '关键字信息
            mKeyword.Clear()
            For Each T As clsKeyWord In .KeyWordList
                mKeyword.Add(T.Word, T)
            Next
            RefreshWordList()
            '注释
            txtSingleComment.Text = .SingleCommentMark
            txtBlockCommentStartMark.Text = .BlockCommentStart
            txtBlockCommentEndMark.Text = .BlockCommentEnd
            lblCommentColor.ForeColor = .CommentColor
            '分词
            lstDelimit.Items.Clear()
            If .SplitMark.Count > 0 Then
                For Each T As String In .SplitMark
                    lstDelimit.Items.Add(T)
                Next
            End If
        End With
    End Sub
    ''' <summary>
    ''' 配置文件的保存操作
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub btnSave_Click() Handles MyOpenSavePanel.Save_Clicked
        With CodeConfig
            '基本信息
            .CodeTitle = txtTitle.Text
            .Language = txtLanguage.Text
            .BackColor = Me.lblBGColor.BackColor
            .TextColor = Me.lblTextColor.ForeColor
            .LeftLimit = nudLeft.Value
            .RightLimit = nudRight.Value
            .RulerColor = Me.lblRulerColor.ForeColor
            .SelectTextColor = Me.lblSelectTextColor.ForeColor
            .SelectBackColor = Me.lblSelectBackColor.BackColor
            '关键字信息
            Dim KeyWordList As New List(Of clsKeyWord)
            KeyWordList.Clear()
            For Each T As clsKeyWord In mKeyword.Values
                KeyWordList.Add(T)
            Next
            .KeyWordList = KeyWordList
            '注释
            .SingleCommentMark = Me.txtSingleComment.Text
            .BlockCommentStart = Me.txtBlockCommentStartMark.Text
            .BlockCommentEnd = Me.txtBlockCommentEndMark.Text
            .CommentColor = Me.lblCommentColor.ForeColor
            '分词
            Dim SplitList As New List(Of String)
            SplitList.Clear()
            If lstDelimit.Items.Count > 0 Then
                For tempCount As Integer = 0 To lstDelimit.Items.Count - 1
                    SplitList.Add(lstDelimit.Items(tempCount).ToString())
                Next
            End If
            .SplitMark = SplitList
        End With
        If FileName = String.Empty Then
            Using mSaveFile As New SaveFileDialog
                mSaveFile.InitialDirectory = Application.StartupPath & "\Config\"
                mSaveFile.Filter = "*.xml(XML)|*.xml"
                If mSaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                    FileName = mSaveFile.FileName
                Else
                    Exit Sub
                End If
            End Using
        End If
        WriteObjectToXMLFile(CodeConfig, GetType(ClsCodeConfig), FileName)
    End Sub
    ''' <summary>
    ''' 多国语言
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub CodeConfigDialog_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '基本信息
        grpBasicInfo.Text = GetTextInLanguageDoc("BasicInfo", TextMode.Text)
        lblConfigName.Text = GetTextInLanguageDoc("ConfigName", TextMode.Text)
        btnBackgroundColor.Text = GetTextInLanguageDoc("BackgroundColor", TextMode.Command)
        btnForeColor.Text = GetTextInLanguageDoc("ForeColor", TextMode.Command)
        lblLanguage.Text = GetTextInLanguageDoc("MultiLanguage", TextMode.Text)
        btnSelectedBackgroundColor.Text = GetTextInLanguageDoc("SelectedBackgroundColor", TextMode.Command)
        btnSelectedForeColor.Text = GetTextInLanguageDoc("SelectedForeColor", TextMode.Command)
        lblVerticalRuler.Text = GetTextInLanguageDoc("VerticalRuler", TextMode.Text)
        lblLeftPosition.Text = GetTextInLanguageDoc("LeftPosition", TextMode.Text)
        lblRightPosition.Text = GetTextInLanguageDoc("RightPosition", TextMode.Text)
        btnVerticalRulerColor.Text = GetTextInLanguageDoc("VerticalRulerColor", TextMode.Text)
        '关键字信息
        grpKeyWordInfo.Text = GetTextInLanguageDoc("KeyWordInfo", TextMode.Text)
        btnKeyWordForeColor.Text = GetTextInLanguageDoc("KeyWordForeColor", TextMode.Command)
        btnDelKeyWord.Text = GetTextInLanguageDoc("DeleteFromList", TextMode.Text)
        btnAddKeyWord.Text = GetTextInLanguageDoc("AddOrModify", TextMode.Text)
        btnSyntaxRule.Text = GetTextInLanguageDoc("SyntaxRule", TextMode.Command)
        lblKeyWord.Text = GetTextInLanguageDoc("KeyWord", TextMode.Text)
        lblToolTips.Text = GetTextInLanguageDoc("ToolTips", TextMode.Text)
        lblSyntaxStructure.Text = GetTextInLanguageDoc("SyntaxStructure", TextMode.Text)
        chkUseForIntellisense.Text = GetTextInLanguageDoc("UseForIntellisense", TextMode.Text)
        '注释
        grpComment.Text = GetTextInLanguageDoc("Comment", TextMode.Text)
        lblSingleComment.Text = GetTextInLanguageDoc("SingleComment", TextMode.Text)
        lblBlockCommentStartMark.Text = GetTextInLanguageDoc("BlockCommentStartMark", TextMode.Text)
        lblBlockCommentEndMark.Text = GetTextInLanguageDoc("BlockCommentEndMark", TextMode.Text)
        btnCommentColor.Text = GetTextInLanguageDoc("CommentColor", TextMode.Text)
        '分隔符
        grpDelimit.Text = GetTextInLanguageDoc("Delimit", TextMode.Text)
        lblDelimit.Text = GetTextInLanguageDoc("Delimit", TextMode.Text)
        btnAddDelimit.Text = GetTextInLanguageDoc("AddToList", TextMode.Text)
        btnDelimitDel.Text = GetTextInLanguageDoc("DeleteFromList", TextMode.Text)
        lblDelimitDesciption.Text = GetTextInLanguageDoc("DelimitDesciption", TextMode.Text)

        'OpenSavePanel
        MyOpenSavePanel.Open_Text = GetTextInLanguageDoc("Open", TextMode.Text)
        MyOpenSavePanel.Save_Text = GetTextInLanguageDoc("Save", TextMode.Text)
        MyOpenSavePanel.Exit_Text = GetTextInLanguageDoc("Exit", TextMode.Text)

        AddHandler MyOpenSavePanel.Exit_Clicked, AddressOf Me.Close
    End Sub
#End Region
#Region "基本信息"
    ''' <summary>
    ''' 背景色的变更
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnBGColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackgroundColor.Click
        Using mColorDialog As New ColorDialog
            mColorDialog.Color = Me.lblBGColor.BackColor
            If mColorDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.lblBGColor.BackColor = mColorDialog.Color
                Me.lblTextColor.BackColor = mColorDialog.Color
                Me.lblRulerColor.BackColor = mColorDialog.Color
                Me.lblKeyWordColor.BackColor = mColorDialog.Color
                Me.lblCommentColor.BackColor = mColorDialog.Color
            End If
        End Using
    End Sub
    ''' <summary>
    ''' 文字色的变更
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnTextColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForeColor.Click
        Using mColorDialog As New ColorDialog
            mColorDialog.Color = Me.lblTextColor.ForeColor
            If mColorDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.lblTextColor.ForeColor = mColorDialog.Color
                Me.lblBGColor.ForeColor = mColorDialog.Color
            End If
        End Using
    End Sub
    Private Sub btnRulerColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerticalRulerColor.Click
        Using mColorDialog As New ColorDialog
            mColorDialog.Color = Me.lblRulerColor.ForeColor
            If mColorDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.lblRulerColor.ForeColor = mColorDialog.Color
            End If
        End Using
    End Sub
    Private Sub btnSelectBackColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectedBackgroundColor.Click
        Using mColorDialog As New ColorDialog
            mColorDialog.Color = Me.lblSelectBackColor.BackColor
            If mColorDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.lblSelectBackColor.BackColor = mColorDialog.Color
                Me.lblSelectTextColor.BackColor = mColorDialog.Color
            End If
        End Using
    End Sub
    Private Sub btnSelectTextColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectedForeColor.Click
        Using mColorDialog As New ColorDialog
            mColorDialog.Color = Me.lblSelectTextColor.ForeColor
            If mColorDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.lblSelectTextColor.ForeColor = mColorDialog.Color
                Me.lblSelectBackColor.ForeColor = mColorDialog.Color
            End If
        End Using
    End Sub
#End Region
#Region "关键字"
    ''' <summary>
    ''' 添加一个关键字
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnKeyWordAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddKeyWord.Click
        If txtKeyWord.Text = String.Empty Then
            '如果关键字为空，警告用户
            MsgBox("请输入关键字")
            Exit Sub
        End If
        Dim Singleword As New clsKeyWord
        Dim strKeyWord As String = txtKeyWord.Text
        If mKeyword.ContainsKey(strKeyWord) Then
            '如果已经存在的话，则先删除
            mKeyword.Remove(strKeyWord)
        End If
        Singleword.Word = strKeyWord
        Singleword.WordColor = Me.lblKeyWordColor.ForeColor
        Singleword.ToolTips = Me.txtToolTips.Text
        Singleword.Intellisense = Me.chkUseForIntellisense.Checked
        Singleword.AutoStructure = Me.txtSyntaxStructure.Text
        mKeyword.Add(strKeyWord, Singleword)
        '刷新关键字列表()
        RefreshWordList()
        Me.txtKeyWord.Text = ""
        Me.txtToolTips.Text = ""
        Me.txtSyntaxStructure.Text = ""
        Me.chkUseForIntellisense.Checked = True
    End Sub
    ''' <summary>
    ''' 删除列表中选中项目
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnKeyWordDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelKeyWord.Click
        If lstKeyWord.SelectedIndex <> -1 Then
            Dim strKeyWord As String = lstKeyWord.SelectedItem.ToString()
            mKeyword.Remove(strKeyWord)
        End If
        '刷新关键字列表()
        RefreshWordList()
    End Sub
    ''' <summary>
    ''' 设定关键字背景色
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnKeyWordColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKeyWordForeColor.Click
        Using mColorDialog As New ColorDialog
            mColorDialog.Color = Me.lblKeyWordColor.ForeColor
            If mColorDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.lblKeyWordColor.ForeColor = mColorDialog.Color
            End If
        End Using
    End Sub
    ''' <summary>
    ''' 通过关键字列表获得关键字信息
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lstKeyWord_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstKeyWord.DoubleClick
        If lstKeyWord.SelectedIndex <> -1 Then
            Dim strKeyWord As String = lstKeyWord.SelectedItem.ToString()
            Dim SingleWord As clsKeyWord = mKeyword(strKeyWord)
            Me.txtKeyWord.Text = SingleWord.Word
            Me.lblKeyWordColor.ForeColor = SingleWord.WordColor
            'XML里的LF,这里要变为CR/LF
            If SingleWord.ToolTips <> String.Empty Then
                Me.txtToolTips.Text = SingleWord.ToolTips.Replace(vbLf, vbCrLf)
            Else
                Me.txtToolTips.Text = ""
            End If
            Me.chkUseForIntellisense.Checked = SingleWord.Intellisense
            If SingleWord.AutoStructure <> String.Empty Then
                Me.txtSyntaxStructure.Text = SingleWord.AutoStructure.Replace(vbLf, vbCrLf)
            Else
                Me.txtSyntaxStructure.Text = ""
            End If
        End If
    End Sub
    ''' <summary>
    ''' 刷新关键字列表
    ''' </summary>
    ''' <remarks></remarks>
    Sub RefreshWordList()
        lstKeyWord.Items.Clear()
        For Each T As clsKeyWord In mKeyword.Values
            lstKeyWord.Items.Add(T.Word)
        Next
    End Sub
    ''' <summary>
    ''' 代码折叠
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdFoldRule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSyntaxRule.Click
        Dim frmSyntaxRule As New SyntaxRule(CodeConfig)
        frmSyntaxRule.ShowDialog()
    End Sub
#End Region
#Region "分词"
    Private Sub btnSplitAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddDelimit.Click
        If txtDelimit.Text = String.Empty Then
            MsgBox("请输入分词符号")
            Exit Sub
        End If
        Dim strSplit As String
        strSplit = txtDelimit.Text.Trim
        If strSplit = String.Empty Then
            txtDelimit.Text = String.Empty
            Exit Sub
        End If
        If Not lstDelimit.Items.Contains(strSplit) Then
            lstDelimit.Items.Add(strSplit)
        End If
        txtDelimit.Text = String.Empty
    End Sub
    Private Sub btnSplitDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelimitDel.Click
        If lstDelimit.SelectedIndex <> -1 Then
            lstDelimit.Items.RemoveAt(lstDelimit.SelectedIndex)
        End If
    End Sub
#End Region
#Region "注释"
    Private Sub btnCommentColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommentColor.Click
        Using mColorDialog As New ColorDialog
            mColorDialog.Color = Me.lblCommentColor.ForeColor
            If mColorDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.lblCommentColor.ForeColor = mColorDialog.Color
            End If
        End Using
    End Sub
#End Region
End Class